//
//  HXDetailVideoVC.m
//  ZCooking
//
//  Created by 韩旭 on 16/9/10.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "HXDetailVideoVC.h"
#import "HX_Macro.h"
#import "HXDetailCycleTCell.h"
#import "HXDetailVideoTCell.h"
#import "UIImage+HXImageColor.h"
#import "HXDetailVideoModel.h"
#import "WMPageController.h"
#import "HXMethodVC.h"
#import "HXMakeFoodVC.h"
#import "HXRelatedVC.h"
#import "HXAntipathyVC.h"

@interface HXDetailVideoVC ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
@property (nonatomic, strong) UIView *sectionView;
@property (nonatomic, strong) UITableView *tableView;
/**
 *  VC容器
 */
@property (nonatomic, strong) UIViewController *showVC;
@property (nonatomic, strong) NSMutableArray *arrStr;
/**
 *  导航的view
 */
@property (nonatomic, strong) UIView *viewNav;
/**
 *  返回按钮
 */
@property (nonatomic, strong) UIButton *leftBtn;
/**
 * 导航的title
 */
@property (nonatomic, strong) UILabel *labelTitle;
/**
 *  传值用的
 */
@property (nonatomic, strong) NSArray *arrNav;
@property (nonatomic, copy) NSString *titleName;
@property (nonatomic, strong) NSMutableArray *detailVideo;
@property (nonatomic, strong) UIViewController *cycleViewController;
@property (nonatomic, strong) WMPageController *wmpageController;
@property (nonatomic, strong) HXDetailCycleTCell *cell;
@property (nonatomic, strong) UIView *lineView;
/**
 *  滚动的视图控制器
 */
@property (nonatomic, strong) UIScrollView *contentView;
@property (nonatomic, strong) UIButton *selectBtn;

@property (nonatomic, strong) UIView *content;
/**
 * 步骤数组
 */
@property (nonatomic, strong) NSMutableArray *step;

@end

@implementation HXDetailVideoVC
- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBarHidden = YES;
}
- (NSMutableArray *)step {
    if (!_step) {
        _step = [NSMutableArray array];
    }
    return _step;
}
- (NSMutableArray *)detailVideo {
    if (!_detailVideo) {
        _detailVideo = [NSMutableArray array];
    }
    return _detailVideo;
}
- (NSMutableArray *)arrStr {
    if (!_arrStr) {
        _arrStr = [NSMutableArray array];
    }
    return _arrStr;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -20, SCREEN_WIDTH, SCREEN_HEIGHT - 50) style:0];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_tableView];
        [_tableView registerNib:[UINib nibWithNibName:@"HXDetailVideoTCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HXDetailVideoTCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"HXDetailCycleTCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HXDetailCycleTCell"];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.arrNav = [NSArray array];
    if (self.dishes_id) {
    NSArray *string = [self.dishes_id componentsSeparatedByString:@"#"];
    for (NSString *str in string) {
        [self.arrStr addObject:str];
    }
        self.ID = self.arrStr[1];
    }
    NSLog(@"%@", self.ID);
    [self getNetWorkingVideoModel:self.ID];
    [self addChildVC];
    [self tableView];
    [self creatHeaderView];
}

- (void)creatContentView {
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.contentView = [[UIScrollView alloc] init];
    _contentView.backgroundColor = [UIColor purpleColor];
    _contentView.contentSize = CGSizeMake(self.childViewControllers.count * SCREEN_WIDTH, 0);
    _contentView.HX_width = SCREEN_WIDTH;
    _contentView.HX_height = 550;
    _contentView.delegate = self;
    _contentView.pagingEnabled = YES;
    [self.cell.contentView insertSubview:_contentView atIndex:0];
    [self scrollViewDidEndScrollingAnimation:_contentView];
}
- (void)addChildVC {
    HXMethodVC *methVC = [[HXMethodVC alloc] init];
    methVC.step = self.step;
    HXMakeFoodVC *makeFood = [[HXMakeFoodVC alloc] init];
    makeFood.dishes_id = self.ID;
    HXRelatedVC *relateVc = [[HXRelatedVC alloc] init];
    relateVc.dishes_id = self.ID;
    HXAntipathyVC *antipath = [[HXAntipathyVC alloc] init];
    antipath.dishes_id = self.ID;
    [self addChildViewController:methVC];
    [self addChildViewController:makeFood];
    [self addChildViewController:relateVc];
    [self addChildViewController:antipath];
}
- (void)creatHeaderView {
    self.viewNav = [[UIView alloc] init];
    _viewNav.HX_width = SCREEN_WIDTH;
    _viewNav.HX_height = 64;
    _viewNav.HX_x = 0;
    _viewNav.HX_y = 0;
    [self.view addSubview:_viewNav];
    self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.leftBtn setImage:[UIImage imageNamed:@"xiangzuoBack1"] forState:UIControlStateNormal];
    _leftBtn.centerY = _viewNav.centerY;
    _leftBtn.HX_x = 20;
    _leftBtn.HX_width = 30;
    _leftBtn.HX_height = 20;
    [self.viewNav addSubview:_leftBtn];
    [self.leftBtn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)back:(UIButton *)btn {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)creatTitleLable {
    self.labelTitle = [[UILabel alloc] init];
    self.labelTitle.text = self.titleName;
    self.labelTitle.backgroundColor = [UIColor clearColor];
    self.labelTitle.HX_width = [SelfSizeTools selfSizeHeightWithString:_labelTitle.text height:self.viewNav.HX_height Font:[UIFont systemFontOfSize:17]];
    self.labelTitle.HX_height = self.viewNav.HX_height;
    self.labelTitle.textColor = [UIColor clearColor];
    self.labelTitle.centerX = self.viewNav.centerX;
    self.labelTitle.centerY = self.leftBtn.centerY;
    [self.viewNav addSubview:_labelTitle];
}
- (void)getNetWorkingVideoModel:(NSString *)dishes_id {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = [NSString stringWithFormat:@"methodName=DishesView&dishes_id=%@&version=4.40&", dishes_id];
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        self.titleName = dicData[@"dishes_name"];
        HXDetailVideoModel *detail = [HXDetailVideoModel ModelWithDic:dicData];
        [self.step addObject:detail];
        [self.detailVideo addObject:dicData];
        [self creatTitleLable];
        [_tableView reloadData];
        [self.arrStr removeAllObjects];
    } Failure:^(NSError *error) {
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HXDetailVideoTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HXDetailVideoTCell"];
        cell.backgroundColor = [UIColor whiteColor];
        HXDetailVideoModel *model = [HXDetailVideoModel ModelWithDic:[self.detailVideo firstObject]];
        [cell.imageHeader sd_setImageWithURL:[NSURL URLWithString:model.image]];
        cell.arrBtn = model.tags_info;
        cell.labelName.text = model.dishes_name;
        cell.labelDetail.text = model.material_desc;
        cell.difficultyBtn.text = [NSString stringWithFormat:@"难度：%@", model.hard_level];
        cell.creatTime.text = [NSString stringWithFormat:@"烹饪时间%@", model.cooke_time];
        cell.taste.text = [NSString stringWithFormat:@"口味：%@", model.taste];
        cell.selectionStyle = NO;
        return cell;
    } else {
        self.cell = [tableView dequeueReusableCellWithIdentifier:@"HXDetailCycleTCell"];
        _cell.backgroundColor = [UIColor whiteColor];
        self.cell.scrollView.delegate = self;
        self.cell.scrollView.contentSize = CGSizeMake(self.childViewControllers.count * SCREEN_WIDTH, 0);
        self.contentView = self.cell.scrollView;
        self.cell.scrollView.pagingEnabled = YES;
        [self scrollViewDidEndScrollingAnimation:self.cell.scrollView];
        return _cell;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        self.sectionView = [[UIView alloc] init];
        _sectionView.backgroundColor = [UIColor redColor];
        NSArray *navTitlte = @[@"做法", @"食材", @"相关常识", @"相宜相克"];
        _lineView = [[UIView alloc] init];
        _lineView.HX_height = 2;
        _lineView.backgroundColor = [UIColor redColor];
        for (NSInteger i = 0; i < 4; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setTitle:navTitlte[i] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:13];
            [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            btn.backgroundColor = [UIColor whiteColor];
            btn.tag = 1000 + i;
            btn.frame = CGRectMake(SCREEN_WIDTH / 4 * i, 0, SCREEN_WIDTH / 4, 40);
            [_sectionView addSubview:btn];
            [btn addTarget:self action:@selector(addChildBtn:) forControlEvents:UIControlEventTouchUpInside];
            [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateDisabled];
            if (i == 0) {
                [self addChildBtn:btn];
                _lineView.HX_width = btn.HX_width;
                _lineView.HX_y = 40 - 2;
            }
        }
        [_sectionView addSubview:_lineView];
        return _sectionView;
    } else {
        return nil;
    }
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    NSInteger i = scrollView.contentOffset.x / SCREEN_WIDTH;
    self.showVC = self.childViewControllers[i];
    _showVC.view.frame = CGRectMake(scrollView.contentOffset.x, 0, SCREEN_WIDTH, 550);
    [scrollView addSubview:_showVC.view];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self scrollViewDidEndScrollingAnimation:scrollView];
    NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
    [self addChildBtn:self.sectionView.subviews[index]];
}
- (void)addChildBtn:(UIButton *)btn {
    self.selectBtn.enabled = YES;
    btn.enabled = NO;
    NSInteger i = btn.tag - 1000;
    [UIView animateWithDuration:0.5 animations:^{
        self.lineView.HX_width = btn.HX_width;
        self.lineView.centerX = btn.centerX;
    }];
    self.selectBtn = btn;
    [self.contentView addSubview:_showVC.view];
    CGFloat index = i * SCREEN_WIDTH;
    [self.contentView setContentOffset:CGPointMake(index, 0) animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else {
        return 40;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HXDetailVideoModel *detailModle = [HXDetailVideoModel ModelWithDic:[self.detailVideo firstObject]];
        CGFloat addHeight = [SelfSizeTools selfSizeHeightWithString:detailModle.material_desc Width:SCREEN_WIDTH - 40 Font:[UIFont systemFontOfSize:13]];
        return 625 + addHeight;
    } else {
        return 500;
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tableView) {
        CGFloat offset_y = scrollView.contentOffset.y;
        CGFloat alpha = (offset_y + 20 - 400) / 200.0f;
        CGFloat colorF = 1 - alpha;
        if (scrollView.contentOffset.y <= 50 && scrollView.contentOffset.y >= 0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y + 20, 0, 0, 0);
        }else if (scrollView.contentOffset.y > 50) {
            scrollView.contentInset = UIEdgeInsetsMake(64 + 20, 0, 0, 0);
        }
        self.viewNav.backgroundColor = [UIColor colorWithWhite:1.000 alpha:alpha];
        self.labelTitle.textColor = [UIColor colorWithWhite:0.108 alpha:alpha];
        UIImageView *imgLeft = [[UIImageView alloc] init];
        imgLeft.image = [UIImage imageNamed:@"xiangzuoBack1"];
        imgLeft.image = [imgLeft.image imageMaskWithColor:[UIColor colorWithWhite:colorF alpha:1.000]];
        [self.leftBtn setImage:imgLeft.image forState:UIControlStateNormal];
    }
}
@end